﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Deployment.Application;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Windows.Forms;

namespace Synergy
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void textBoxTm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar==13)
            {
                CheckIn();
                
            }
        }

        private void CheckIn()
        {
            
            //if (DateTime.Compare(DateTime.Now, Convert.ToDateTime("2024-02-27")) < 0)
            //{
                string sql;
                string username;

                username = this.textBoxTm.Text.Trim().Replace("--", "");
                if (username == pub.adminname())
                {
                    pub.LoginUser = username;
                    Form mainfrm = new FormMain();
                    mainfrm.Show();
                    this.Hide();
                }
                else
                {
                    //sql = "select * from Z_UA_User where cUSER_Id='" + username + "'";
                sql = $@" select * from Z_UA_User left join  Z_UserPassword
                          on Z_UserPassword.cUSER_Id = Z_UA_User.cUSER_Id
                          where Z_UA_User.cUSER_Id = '{username}' and Z_UserPassword.password = '{textBox密码.Text.Trim()}'";
                DataTable tb = pub.SysGetTable(sql);
                if (tb.Rows.Count > 0)
                {
                    pub.LoginUser = username;
                    Form mainfrm = new FormMain();
                    mainfrm.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("用户名或密码错误。如果还没设置过密码，请先设置密码");
                }
                //}
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CheckIn();
            pub.mac = MAC.getIP.getLocalMac();
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            this.textBoxTm.Text = "";
            this.textBoxTm.Focus();
        }

        private void Login_Load(object sender, EventArgs e)
        {
            this.Text += "-" + Application.ProductVersion;
            this.label3.Text = GetVersion();
            string oldversion = GetVersion();
            //获取程序名
            string appName = "特种仪表";
            string sql = $"select 版本号,url from AppVersion where 程序类型 = '{appName}'";
            DataTable version = SqlSugarHelp.scope.Ado.GetDataTable(sql);
            if (oldversion != version.Rows[0][0].ToString())
            {
                //提示有更新要求升级
                DialogResult dialogResult = MessageBox.Show($"有新版本是否更新。当前版本{oldversion},新版本{version.Rows[0][0].ToString()}", "提示", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    更新(version.Rows[0][1].ToString(), appName);
                }
                else
                {
                    Application.Exit();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ////检查更新
            //string oldversion = GetVersion();
            //label3.Text = oldversion;
            ////获取程序名
            string appName = "特种仪表";
            string sql = $"select 版本号,url from AppVersion where 程序类型 = '{appName}'";
            DataTable version = SqlSugarHelp.scope.Ado.GetDataTable(sql);
            //if (oldversion != version.Rows[0][0].ToString())
            //{
            //    //提示有更新要求升级
            //    DialogResult dialogResult = MessageBox.Show($"有新版本是否更新。当前版本{oldversion},新版本{version.Rows[0][0].ToString()}", "提示", MessageBoxButtons.YesNo);
            //    if (dialogResult == DialogResult.Yes)
            //    {
            更新(version.Rows[0][1].ToString(), appName);
            //    }
            //    else
            //    {
            //        Application.Exit();
            //    }
            //}
        }

        public string GetVersion()
        {
            //注意在调试的时候是获取不到版本号的，需要程序发布之后才能获得
            return ApplicationDeployment.IsNetworkDeployed
            ? ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString()
            : System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }
        private void 更新(string url, string appName)
        {
            //运行当前目录的上级目录里的 合同管理下载更新.exe 程序
            string applicationPath = Application.StartupPath;
            applicationPath = Path.GetFullPath("..");
            string consoleAppPath = Path.Combine(applicationPath, "下载更新.exe");
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = consoleAppPath;
            startInfo.Arguments = $"{url}" + " " + $"{appName}";
            Process.Start(startInfo);
            // 结束当前 WinForms 进程
            Application.Exit();
        }

        private void button修改密码_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.textBoxTm.Text.Trim().Replace("--", "")) == true)
            {
                MessageBox.Show("请先扫工号");
                return;
            }
            Form修改密码 form = new Form修改密码();
            form.工号 = this.textBoxTm.Text.Trim().Replace("--", "");
            form.ShowDialog();
        }
    }
}
